/****************************************************************
 * <strong>MFURLManager</strong>
 *
 * <p>
 * MFURLManager
 * </p>
 *
 * @author AndyZhang 2012  cnet2001@163.com
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.filter;

import com.thinkwide.data.core.MFString;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MFURLManager {
    private List<String> includeFiles;

    private Map<String, String> configs;
    private Map<String, String> urls;

    public MFURLManager() {
        includeFiles = new ArrayList<String>();
        configs = new LinkedHashMap<String, String>();
        urls = new LinkedHashMap<String, String>();
    }


    public String getAttrValue(Element item, String name) {
        Attribute attr = item.attribute(name);
        if (attr != null) {
            String text = attr.getText();
            return text;
        }
        return null;
    }

    public String getNodeValue(Element item, String name) {
        Element elem = item.element(name);
        if (elem != null) {
            String text = elem.getStringValue();
            return text;
        }
        return null;
    }

    public boolean loadConfig(String filePath) throws DocumentException {
        File file = new File(filePath);

        SAXReader reader = new SAXReader();
        Document doc = reader.read(file);
        Element root = doc.getRootElement();

        //load include file
        for (Iterator<?> i = root.elementIterator("include"); i.hasNext(); ) {
            Element item = (Element) i.next();
            String value = getAttrValue(item, "file");
            if (MFString.notNullOrEmpty(value))
                includeFiles.add(value);
        }

        for (Iterator<?> i = root.elementIterator("constant"); i.hasNext(); ) {
            Element item = (Element) i.next();

            String conName = getAttrValue(item, "name");
            String conValue = getAttrValue(item, "value");
            if (MFString.notNullOrEmpty(conName) && MFString.notNullOrEmpty(conValue)) {
                configs.put(conName, conValue);
            }
        }


        for (Iterator<?> i = root.elementIterator("url"); i.hasNext(); ) {
            Element urlItem = (Element) i.next();
            String type = getAttrValue(urlItem, "type");
            String url = urlItem.getStringValue();
            urls.put(url, type);
        }
        return true;

    }

    public boolean needUser(String url) {
        for (Map.Entry<String, String> item : urls.entrySet()) {
            //Pattern pattern = Pattern.compile(item.getKey());
            //Matcher matcher=pattern.matcher(url);
            //if (matcher.find())
            //	return (MFString.compareNoCase(item.getValue(),"check"));
            //MFDebug.println(url);
            //MFDebug.println(item.getKey());
            if (url.startsWith(item.getKey())) {
                return false;
            }
        }

        return true;
    }
}
